package com.softcloud.gamesbase;

import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;


public class LoginActivity extends BaseActivity {

    private EditText username;
    private EditText password;
    private CheckBox isRemember;
    private Button login;

    private SharedPreferences pref;
    private SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        username = (EditText)findViewById(R.id.username);
        password = (EditText)findViewById(R.id.password);
        isRemember = (CheckBox)findViewById(R.id.is_remember);
        login = (Button)findViewById(R.id.login_button);

        pref = PreferenceManager.getDefaultSharedPreferences(this);
        Boolean remember = pref.getBoolean("remember",false);
        editor = pref.edit();
        if(remember){
            String inputUsername = pref.getString("username","");
            String inputPassword = pref.getString("password","");
            username.setText(inputUsername);
            password.setText(inputPassword);
            isRemember.setChecked(true);
        }

        LoginButtonListener loginButtonListener = new LoginButtonListener();
        login.setOnClickListener(loginButtonListener);


    }

    class LoginButtonListener implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            String inputUsername = username.getText().toString();
            String inputPassword = password.getText().toString();
            if(inputUsername.equals("Softcloud")&&inputPassword.equals("123456")){
                if(isRemember.isChecked()){
                    editor.putString("username",inputUsername);
                    editor.putString("password",inputPassword);
                    editor.putBoolean("remember",true);
                }else{
                    editor.clear();
                }
                editor.commit();
                Intent intent = new Intent(LoginActivity.this,MainPage.class);
                startActivity(intent);
                finish();
            }else {
                Toast.makeText(LoginActivity.this,"用户名或密码错误",Toast.LENGTH_SHORT).show();
            }
        }
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_login, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
